<?php
!defined('IN_NOVA') && exit('Access Denied!');

class Vcode {

	private $sessionvar = 'vdcode';	//Session变量名称

	public function show( $code_name = 'vdcode', $code_type = 1, $width = 50, $height = 20 ) {
		$this->sessionvar = $code_name;

		if (function_exists('imagecreate')) {

			//数字+字符的验证码
			if ( $code_type == 1 ) {

				//产生4个字符的随机字符串
				$str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
				$len = strlen($str);
				$code = array();
				for ($i=0; $i<4; $i++) {
					$code[] = $str{mt_rand(0,$len-1)};
				}

				$_SESSION[$this->sessionvar] = strtolower(implode('',$code));

				$img = ImageCreate($width,$height);	//创建图形
				ImageColorAllocate($img,255,255,255); //填充背景

				//添加杂色
				for ($i=0; $i<100; $i++) {
					$pxcolor = ImageColorAllocate($img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
					ImagesetPixel($img,mt_rand(0,$width),mt_rand(0,$height),$pxcolor);
				}

				//绘制边框
				$bordercolor = ImageColorAllocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
				ImageRectangle($img,0,0,$width-1,$height-1,$bordercolor);

				//写上验证码文字
				$offset = round($width/4/2);
				$h1 = 0;
				$h2 = round($height/3);
				foreach ($code as $char) {			
					$textcolor = ImageColorAllocate($img,mt_rand(0,250),mt_rand(0,150),mt_rand(0,250));
					ImageChar($img,5,$offset + mt_rand(-5,round($width/4/3)), mt_rand($h1, $h2),$char,$textcolor);
					$offset += round($width/4)-3;
				}
	
			//简单数学运算验证码
			} elseif ( $code_type == 2 ) {

				$operator = '+-*';	//运算符

				$code = array();
				$code[] = mt_rand(1,9);
				$code[] = $operator{mt_rand(0,2)};
				$code[] = mt_rand(1,9);
				$code[] = $operator{mt_rand(0,2)};
				$code[] = mt_rand(1,9);
				$codestr = implode('',$code);
				eval("\$result = ".implode('',$code).";");
				$code[] = '=';

				$_SESSION[$this->sessionvar] = $result;

				$img = ImageCreate($width,$height);
				ImageColorAllocate($img, mt_rand(230,250), mt_rand(230,250), mt_rand(230,250));
				$color = ImageColorAllocate($img, 0, 0, 0);

				$offset = round($width/6/2);
				$h1 = 0;
				$h2 = round($height/6);
				foreach ($code as $char) {
			
					$txtcolor = ImageColorAllocate($img, mt_rand(0,255), mt_rand(0,150), mt_rand(0,255));
					ImageChar($img, mt_rand(3,5), $offset + mt_rand(-5,round($width/6/3)), mt_rand($h1, $h2), $char, $txtcolor);
					$offset += round($width/6)-3;

				}
				for ($i=0; $i<100; $i++) {
			
					$pxcolor = ImageColorAllocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
					ImagesetPixel($img, mt_rand(0,$width), mt_rand(0,$height), $pxcolor);
				}

			}

			header("pragma:no-cache\r\n");
			header("Cache-Control:no-cache\r\n");
			header("Expires:0\r\n");
			//检查系统支持的文件类型，优先级为PNG->JPEG->GIF
			if (Imagetypes() & IMG_PNG) {
				header('content-type:image/png');
				ImagePNG($img);
			} elseif (Imagetypes() & IMG_JPEG) {
				header('content-type:image/jpeg');
				ImageJPEG($img);
			} else {
				header('content-type:image/gif');
				ImageGif($img);
			}
		}
	}

	public function check( $code ) {
		if( !isset( $_SESSION[$this->sessionvar] ) ) {
			return false;
		}
		if( $code != $_SESSION[$this->sessionvar] ) {
			unset( $_SESSION[$this->sessionvar] );
			return false;
		} else {
			unset( $_SESSION[$this->sessionvar] );
			return true;
		}
	}

}
?>
